package arr;

import list.AbstractList;
import list.List;

/**
 * @description:
 * @author: qian
 * @createDate: 2021/9/13
 */
public class ArrayList<E> extends AbstractList<E> {


    // 动态数组
    private E[] elements;

    // 默认的容量
    private final static int DEFAULT_CAPACITY = 10;

    // 没有该元素返回-1
    private final static int ELEMENT_NOT_FOUND = -1;


    /**
     * 指定的容量小于默认容量就用默认容量
     * @param capacity
     */
    public ArrayList(int capacity) {
        capacity = (capacity < DEFAULT_CAPACITY) ? DEFAULT_CAPACITY : capacity;
        this.elements = (E[]) new Object[capacity];

    }

    /**
     * 不指定容量那就是默认
     */
    public ArrayList() {
        this(DEFAULT_CAPACITY);
    }


    // 是否包含某个元素
    public boolean contains(E element) {

        return indexOf(element) != ELEMENT_NOT_FOUND;


    }


    /**
     * 返回index位置对应的元素
     * @param index
     * @return
     */
    public E get(int index) {

        rangeCheck(index);

        return elements[index];

    }
    // 设置index位置的元素
    public E set(int index, E element) {

        rangeCheck(index);

        E old = elements[index];
        elements[index] = element;
        return old;
    }
    // 往index位置添加元素
    public void add(int index, E element) {

        rangeCheckForAdd(index);

        ensureCapacity(size + 1);

        for (int i = size - 1; i >= index; i--) {
            elements[i + 1] = elements[i];
        }
        elements[index] = element;
        size++;

    }


    /**
     * 扩容
     * @param capacity 保证至少有capacity个元素
     */
    private void ensureCapacity(int capacity) {

        int oldCapacity = elements.length;
        if (oldCapacity >= capacity) return;

        // 扩容1.5倍 位运算效率特别高
        int newCapacity = oldCapacity + (oldCapacity >> 1);
        E[] newElements = (E[]) new Object[newCapacity];
        for (int i = 0; i < size; i++) {
            newElements[i] = elements[i];
        }
        elements = newElements;
        System.out.println(oldCapacity + "扩容为" + newCapacity);
    }

    // 删除index位置对应的元素
    public E remove(int index) {

        rangeCheck(index);
        E old = elements[index];
        for (int i = index + 1; i <= size - 1; i++) {

            elements[i - 1] = elements[i];
        }
        size --;

        elements[size] = null;
        return old;

    }
    // 查看元素的位置
    public int indexOf(E element) {

        if (element == null) {
            for(int i = 0; i < size; i++) {
                if (elements[i] == null) {
                    return i;
                }
            }
        } else {
            for(int i = 0; i < size; i++) {
                if (elements[i].equals(element)) {
                    return i;
                }
            }
        }

        return ELEMENT_NOT_FOUND;

    }
    // 清除所有元素
    public void clear() {
        for (int i = 0; i < size; i++) {
            elements[i] = null;
        }
        size = 0;
    }



    @Override
    public String toString() {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("size = ").append(size).append(" , [");

        for (int i = 0; i < size; i++) {
            if (i != 0) {
                stringBuilder.append(", ");
            }
            stringBuilder.append(elements[i]);

        }
        stringBuilder.append("]");
        return stringBuilder.toString();
    }
}
